import { createWebHistory, createRouter } from 'vue-router'
import Layout from '@/layout'

// 公共路由
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index.vue')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login'),
    hidden: true
  },
  {
    path: '/sociallogin',
    component: () => import('@/views/socialLogin'),
    hidden: true
  },
  {
    path: '/register',
    component: () => import('@/views/register'),
    hidden: true
  },
  {
    path: '/:pathMatch(.*)*',
    component: () => import('@/views/error/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error/401'),
    hidden: true
  },
  {
    path: '/error',
    component: () => import('@/views/error/Error'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/workbench',
    children: [
      {
        path: 'workbench',
        component: () => import('@/pages/workbench/Workbench.vue'),
        name: 'workbench',
        meta: {
          title: "首页工作台",
          icon: "dashboard"
        }
      }
    ]
  },
  {
    path: '/production',
    name: 'Production',
    component: Layout,
    meta: { title: '生产管理', icon: 'production' },
    children: [
      {
        path: 'customer-order',
        component: () => import('@/pages/production/CustomerOrder.vue'),
        name: 'CustomerOrder',
        meta: { title: '客户订单' }
      },
      {
        path: 'production-dispatch',
        component: () => import('@/pages/production/ProductionDispatch.vue'),
        name: 'ProductionDispatch',
        meta: { title: '生产派单' }
      },
      {
        path: 'production-report',
        component: () => import('@/pages/production/ProductionReport.vue'),
        name: 'ProductionReport',
        meta: { title: '生产报工' }
      }
    ]
  },
  {
    path: '/storage',
    name: 'Storage',
    component: Layout,
    meta: { title: '出入库管理', icon: 'storage' },
    children: [
      {
        path: 'sn-code',
        component: () => import('@/pages/storage/SNCode.vue'),
        name: 'SNCode',
        meta: { title: 'SN码' }
      },
      {
        path: 'product-in',
        component: () => import('@/pages/storage/ProductIn.vue'),
        name: 'ProductIn',
        meta: { title: '产品入库' }
      },
      {
        path: 'product-check',
        component: () => import('@/pages/storage/ProductCheck.vue'),
        name: 'ProductCheck',
        meta: { title: '出货检验' }
      }
    ]
  },
  {
    path: '/quality',
    name: 'Quality',
    component: Layout,
    meta: { title: '质量管理', icon: 'quality' },
    children: [
      {
        path: 'detection-template',
        name: 'DetectionTemplate',
        component: () => import('@/pages/quality/DetectionTemplate.vue'),
        meta: { title: '检测模板' }
      },
      {
        path: 'common-defect',
        name: 'CommonDefect',
        component: () => import('@/pages/quality/CommonDefect.vue'),
        meta: { title: '常见缺陷' }
      },
      {
        path: 'inspection-task',
        name: 'InspectionTask',
        component: () => import('@/pages/quality/InspectionTask.vue'),
        meta: { title: '待检任务' }
      },
      {
        path: 'maintenance-order',
        name: 'MaintenanceOrder',
        component: () => import('@/pages/quality/MaintenanceOrder.vue'),
        meta: { title: '维修任务' }
      }
    ]
  },
  {
    path: '/process',
    name: 'Process',
    component: Layout,
    meta: { title: '上料与工艺管理', icon: 'process' },
    children: [
      {
        path: 'material-info',
        component: () => import('@/pages/process/MaterialInfo.vue'),
        name: 'MaterialInfo',
        meta: { title: '物料基础档' }
      },
      {
        path: 'production-pick',
        component: () => import('@/pages/process/ProductionPick.vue'),
        name: 'ProductionPick',
        meta: { title: '生产领料单' }
      },
      {
        path: 'process-route',
        component: () => import('@/pages/process/ProcessRoute.vue'),
        name: 'ProcessRoute',
        meta: { title: '工艺路线' }
      }
    ]
  },
  {
    path: '/workstation',
    component: Layout,
    name: 'Workstation',
    children: [
      {
        path: '',
        component: () => import('@/pages/Workstation.vue'),
        name: 'WorkstationInfo',
        meta: { title: '工作站管理', icon: 'workstation' },
      }
    ]
  }

]

const router = createRouter({
  history: createWebHistory(import.meta.env.VITE_APP_ROUTER_PREFIX),
  routes: constantRoutes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }
})

export default router
